﻿<%@ WebHandler Language="C#" Class="Assesment.Web.AjaxHandler.Transaction.AssesmentPositionHandler" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

using Assesment.Library.DataAccess.Administration;
using Assesment.Library.DomainObject.Administration;
using Assesment.Library.DataAccess.Administration.Catalog;
using Assesment.Library.DataAccess.Transaction;
using Assesment.Library.DomainObject.Transaction;
using Training.Library.DataAccess.Administration.Catalog;
using Training.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;

using Assesment.Library.Enum;
using Assesment.Web.Support;
using Newtonsoft.Json.Linq;
namespace Assesment.Web.AjaxHandler.Transaction
{

    public class AssesmentPositionHandler : Assesment.Web.BaseHandler
    {
        private  AssesmentPositionRepo AssesmentPositionDal = new AssesmentPositionRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
        private KaryawanRepo KaryawanDal = new KaryawanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
        private PositionRepo PositionDal = new PositionRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);
        private SIPO.Library.DataAccess.Administration.Catalog.CompetencyRepo CompetencyDal = new SIPO.Library.DataAccess.Administration.Catalog.CompetencyRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);
        private recLogRepo logDal = new recLogRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
        private string MethodName = string.Empty;

        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);
            MethodName = context.Request.Params["method"];
            context.Response.Clear();
            context.Response.Buffer = true;

            System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream);
            string jsonString = string.Empty;
            jsonString = context.Server.UrlDecode(sr.ReadToEnd());
            JObject jObj = string.IsNullOrEmpty(jsonString) ? null : JObject.Parse(jsonString);

            sr.Close();
            sr.Dispose();

            switch (MethodName.ToLower())
            {
                case ("insert"):
                    context.Response.Write(Insert(context, jObj));
                    break;
                case ("update"):
                    context.Response.Write(Update(context, jObj));
                    break;
                case ("delete"):
                    context.Response.Write(Delete(context, jObj));
                    break;
                case ("getdata"):
                    context.Response.Write(GetData(context));
                    break;

                default:
                    // do nothing
                    break;
            }

            context.Response.Flush();
            context.Response.End();
        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string Insert(HttpContext context, JObject jo)
        {
            JSONHelper helper = new JSONHelper();

            try
            {

                DateTime TglAssesment = Helper.FormatDate(jo["AssesmentDate"].ToString(), Helper.Culture.Indonesian);
                Karyawan objKaryawan = KaryawanDal.GetById((string)jo["NIK"]);
                AssesmentPosition obj = new AssesmentPosition();
                obj.NIK = (string)jo["NIK"];
               
                if (objKaryawan != null)
                {
                    obj.PositionID = objKaryawan.PositionID;
                    Position objPosition = PositionDal.GetById(Convert.ToInt32(objKaryawan.PositionID));
                    if (objPosition != null)
                        obj.DepartmentID = objPosition.DepartmentID;
                }
                
              
                obj.PositionIDAssesment = (int)jo["PositionIDAssesment"];
                obj.DepartmentIDAssesment = (int)jo["DepartmentIDAssesment"];
                obj.AssesmentDate = TglAssesment;
                obj.TotalFitID = (int)jo["TotalFitID"];
                obj.PersonalDescription = (string)jo["PersonalDescription"];
                obj.PersonalityID = (int)jo["PersonalityID"];
                obj.LeadershipID = (int)jo["LeadershipID"];
                
                obj.CreatedDate = DateTime.Now;
                obj.CreatedBy = base.UserLoggedIn;

                List<SIPO.Library.DomainObject.Administration.Catalog.Competency> listCompetency = CompetencyDal.GetList();
                var queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("CUSTOMER FOCUS"));

                int IdCompetency = 0;
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency= data.ComptID;
                }

                List<AssesmentPositionDetail> listDetail = new List<AssesmentPositionDetail>();
                AssesmentPositionDetail objDetail = new AssesmentPositionDetail();

                #region Group Customer Focus
                
                string KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDCustomerFocus"];
                string[] arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                string StatusKeyBehaviour = (string)jo["StatusCheckListCustomerFocus"];
                string[] arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

              

                for (int i = 0; i < arrKeyBehaviourDetailID.Length-1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingCustomerFocus"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion


                #region Group Integrity

                 KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDIntegrity"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListIntegrity"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("INTEGRITY"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }
                
                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingIntegrity"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion


                #region Group Team Work

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDTeamWork"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListTeamWork"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("TEAM WORK"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingTeamWork"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion

                #region Group Continous Learning

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDContinous"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListContinous"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("CONTINOUS LEARNING"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingContinous"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion

                #region Group Work Excellence

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDWorkExcellence"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListWorkExcellence"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("WORK EXCELLENCE"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingWorkExcellence"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion


                #region Group Financial Accument

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDFinancial"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListFinancial"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("FINANCIAL"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingFinancial"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion


                #region Group Bussines Partnership

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDBussines"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListBussines"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("PARTNERSHIP"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingBussines"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion


                #region Group Leadership

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDLeadership"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListLeadership"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("LEADERSHIP"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingLeadership"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion

                #region Group Resource Management

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDResource"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListResource"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("RESOURCE MANAGEMENT"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingResource"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion




                AssesmentPositionDal.Save(obj,listDetail);

                helper.Message = "Data berhasil di simpan !";
                helper.IsSuccess = true;

            }
            catch (Exception ex)
            {

                helper.Message = ex.Message;
                helper.IsSuccess = false;
            }
            finally
            {
                recLog log = new recLog();
                log.LogEvent = "Insert";
                log.LogMessage = helper.Message;
                log.LogSource = "AssesmentPositionhandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);

            }

            return Newtonsoft.Json.JsonConvert.SerializeObject(helper);

        }

        private string Update(HttpContext context, JObject jo)
        {
            JSONHelper helper = new JSONHelper();

            try
            {

                DateTime TglAssesment = Helper.FormatDate(jo["AssesmentDate"].ToString(), Helper.Culture.Indonesian);
                Karyawan objKaryawan = KaryawanDal.GetById((string)jo["NIK"]);
                AssesmentPosition obj = new AssesmentPosition();
                obj.NIK = (string)jo["NIK"];
                obj.AssesmentPositionID = (int)jo["AssesmentPositionID"];
                if (objKaryawan != null)
                {
                    obj.PositionID = objKaryawan.PositionID;
                    Position objPosition = PositionDal.GetById(Convert.ToInt32(objKaryawan.PositionID));
                    if (objPosition != null)
                        obj.DepartmentID = objPosition.DepartmentID;
                }


                obj.PositionIDAssesment = (int)jo["PositionIDAssesment"];
                obj.DepartmentIDAssesment = (int)jo["DepartmentIDAssesment"];
                obj.AssesmentDate = TglAssesment;
                obj.TotalFitID = (int)jo["TotalFitID"];
                obj.PersonalDescription = (string)jo["PersonalDescription"];
                obj.PersonalityID = (int)jo["PersonalityID"];
                obj.LeadershipID = (int)jo["LeadershipID"];
                
                obj.UpdatedDate = DateTime.Now;
                obj.UpdatedBy = base.UserLoggedIn;

                List<SIPO.Library.DomainObject.Administration.Catalog.Competency> listCompetency = CompetencyDal.GetList();
                var queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("CUSTOMER FOCUS"));

                int IdCompetency = 0;
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                #region Group Customer Focus

                string KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDCustomerFocus"];
                string[] arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                string StatusKeyBehaviour = (string)jo["StatusCheckListCustomerFocus"];
                string[] arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                List<AssesmentPositionDetail> listDetail = new List<AssesmentPositionDetail>();
                AssesmentPositionDetail objDetail = new AssesmentPositionDetail();

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingCustomerFocus"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion


                #region Group Integrity

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDIntegrity"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListIntegrity"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("INTEGRITY"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingIntegrity"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion


                #region Group Team Work

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDTeamWork"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListTeamWork"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("TEAM WORK"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingTeamWork"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion

                #region Group Continous Learning

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDContinous"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListContinous"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("CONTINOUS LEARNING"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingContinous"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion

                #region Group Work Excellence

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDWorkExcellence"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListWorkExcellence"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("WORK EXCELLENCE"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingWorkExcellence"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion


                #region Group Financial Accument

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDFinancial"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListFinancial"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("FINANCIAL"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingFinancial"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion


                #region Group Bussines Partnership

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDBussines"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListBussines"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("PARTNERSHIP"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingBussines"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion


                #region Group Leadership

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDLeadership"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListLeadership"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("LEADERSHIP"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingLeadership"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion

                #region Group Resource Management

                KeyBehaviourDetailID = (string)jo["KeyBehaviourDetailIDResource"];
                arrKeyBehaviourDetailID = KeyBehaviourDetailID.Split('|');

                StatusKeyBehaviour = (string)jo["StatusCheckListResource"];
                arrStatusKeyBehaviour = StatusKeyBehaviour.Split('|');

                queryCustomerFocus = listCompetency.Where(x => x.CompetncyDesc.ToUpper().Contains("RESOURCE MANAGEMENT"));
                foreach (SIPO.Library.DomainObject.Administration.Catalog.Competency data in queryCustomerFocus.ToList())
                {
                    IdCompetency = data.ComptID;
                }

                for (int i = 0; i < arrKeyBehaviourDetailID.Length - 1; i++)
                {
                    objDetail = new AssesmentPositionDetail();
                    objDetail.CompetencyID = IdCompetency;
                    objDetail.KeyBehaviourDetailID = Convert.ToInt32(arrKeyBehaviourDetailID[i]);
                    objDetail.StatusKeyBehaviour = arrStatusKeyBehaviour[i].ToString().Trim() == "1" ? true : false;
                    objDetail.Rating = (int)jo["RatingResource"];
                    objDetail.CreatedBy = base.UserLoggedIn;
                    objDetail.CreatedDate = DateTime.Now;

                    listDetail.Add(objDetail);
                }

                #endregion


                AssesmentPositionDal.Update(obj, listDetail);
                
                helper.Message = "Data berhasil di ubah !";
                helper.IsSuccess = true;
            }
            catch (Exception ex)
            {
                helper.IsSuccess = false;
                helper.Message = ex.Message;
            }
            finally
            {
                recLog log = new recLog();
                log.LogEvent = "Update";
                log.LogMessage = helper.Message;
                log.LogSource = "AssesmentPositionhandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);
            }

            return Newtonsoft.Json.JsonConvert.SerializeObject(helper);

        }

        private string Delete(HttpContext context, JObject jo)
        {
            JSONHelper helper = new JSONHelper();

            try
            {
                var strId = (string)jo["ID"];

                string[] ids = strId.Split(',');
                foreach (string id in ids)
                {
                    int intID = Convert.ToInt32(id);
                    AssesmentPositionDal.DeleteWithDetail(intID);
                }

                helper.IsSuccess = true;
                helper.Message = "Data berhasil di hapus !";
            }
            catch (Exception ex)
            {
                helper.IsSuccess = false;
                helper.Message = ex.Message;
            }
            finally
            {
                recLog log = new recLog();
                log.LogEvent = "Delete";
                log.LogMessage = helper.Message;
                log.LogSource = "AssesmentPositionhandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);

            }

            return Newtonsoft.Json.JsonConvert.SerializeObject(helper);

        }

        private string GetData(HttpContext context)
        {
            string parameters = Helper.JqGridFilterToSQLParams(context);

            int page = 1;
            int.TryParse(context.Request.QueryString["page"] ?? "1", out page);

            int pageSize = 10;
            int.TryParse(context.Request.QueryString["rows"] ?? "10", out pageSize);

            string sidx = ((context.Request.QueryString["sidx"]) == null ? AssesmentPosition.Fields.AssesmentPositionID : context.Request.QueryString["sidx"].ToString());

            string sord = ((context.Request.QueryString["sord"]) == null ? "ASC" : context.Request.QueryString["sord"].ToString());

            int totalCount = 0;
            int totalPages = 0;
            List<AssesmentPosition> pagedList = null;

            try
            {
                totalCount = Convert.ToInt32(AssesmentPositionDal.SelectScalar(SQL.Function.Aggregate.Count, AssesmentPosition.Fields.AssesmentPositionID, parameters));

                decimal dc = decimal.Ceiling(Convert.ToDecimal(totalCount) / Convert.ToDecimal(pageSize));
                totalPages = Convert.ToInt32(dc);

                pagedList = AssesmentPositionDal.GetList(pageSize,page, sidx, sord, parameters);
            }
            catch (Exception ex)
            {
                recLog log = new recLog();
                log.LogEvent = "GetData";
                log.LogMessage = ex.Message;
                log.LogSource = "AssesmentPositionhandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);
                log.Dispose();
            }
            finally
            {

            }

            // send params into jqGrid's format
            var gridData = new
            {
                page = page,
                total = totalPages,
                records = totalCount,
                rows = pagedList,
            };

            return Newtonsoft.Json.JsonConvert.SerializeObject(gridData);

        }

    }

}